package cn.nabaixin.discover;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import cn.nabaixin.R;
import cn.nabaixin.setting.SettingActivity;
import cn.nabaixin.utils.AppUtils;

public class DiscoverActivity extends ListActivity {

	private ListView lv;
	private SimpleAdapter sa;
	private List<Map<String, Object>> data;
	private TextView refreshView;
	private TextView refreshTimeView;
	private View headerView;
	private Context c;
	
	//菜单项
	public final int MENU_REFRESH = 1;
	public final int MENU_TOTOP = 2;
	public final int MENU_SETTING = 3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		c = DiscoverActivity.this;
		initViews();
		initListener();
		initData();
	}
	
	/**
	 * 初始化界面
	 */
	private void initViews(){
		lv = getListView();
		lv.setBackgroundColor(Color.WHITE);
		lv.setCacheColorHint(0);
		headerView = getLayoutInflater().inflate(R.layout.latest_list_header, null);
		ImageView adImg = (ImageView) headerView.findViewById(R.id.discover_adImg);
		adImg.setVisibility(View.VISIBLE);
		TextView itemTitle = (TextView)headerView.findViewById(R.id.discover_itemTitle);
		itemTitle.setVisibility(View.VISIBLE);
		lv.addHeaderView(headerView);
	}
	private void initListener(){
		View.OnClickListener clickListener = new ClickListener();
		refreshView = (TextView) headerView.findViewById(R.id.latest_list_refresh);
		refreshTimeView = (TextView) headerView.findViewById(R.id.latest_list_refershTime);
		refreshView.setOnClickListener(clickListener);
		refreshTimeView.setOnClickListener(clickListener);
	}
	/**
	 * 初始化数据
	 */
	private void initData(){
		//初始化静态数据
		String names[] = new String[]{"张三", "李四", "王五"};
		String positions[] = new String[]{"北京西直门", "上地城铁", "时代之光1号楼"};
		String contents[] = new String[]{"状态好才是真的好!状态好才是真的好！", "好好学习天天向上！哈哈哈哈哈哈哈哈！！！！！", "招行行长:房价上涨是因为百姓手中钱太多？？？"};
		String sendTiems[] = new String[]{"一周前", "两天前", "一天前", "上午", };
		String distances[] = new String[]{"距离600米", "距离100米", "距离200米", "距离300米", "距离400米", "距离500米"};
		
		data = new ArrayList<Map<String,Object>>();
		data.add(null);
		for(int i=0; i<7; i++){
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("name", AppUtils.getRandomItem(names));
			item.put("position", AppUtils.getRandomItem(positions));
			item.put("content", AppUtils.getRandomItem(contents));
			item.put("sendtime", AppUtils.getRandomItem(sendTiems));
			item.put("distance", AppUtils.getRandomItem(distances));
			data.add(item);
		}
		
		sa = new SimpleAdapter(c, data, R.layout.latest_list_item, 
				new String[]{"name", "position", "content", "sendtime", "distance"}, 
				new int[]{R.id.user_name, R.id.user_position, R.id.user_content, R.id.user_sendTime, R.id.user_distance});
		lv.setAdapter(sa);
	}
	
	//添加菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_REFRESH, 0, "刷新").setIcon(R.drawable.menu_refresh);
		menu.add(0, MENU_TOTOP, 1, "会顶部").setIcon(R.drawable.menu_gotop);
		menu.add(0, MENU_SETTING, 2, "设置").setIcon(R.drawable.menu_setting);
		return super.onCreateOptionsMenu(menu);
	}
	//处理菜单点击事件
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		switch (itemId) {
		case MENU_REFRESH:
			Toast.makeText(c, "MENU_REFRESH", 0).show();
			return true;
		case MENU_TOTOP:
			Toast.makeText(c, "MENU_FILTER", 0).show();
			return true;
		case MENU_SETTING:
			Intent intent2 = new Intent(c, SettingActivity.class);
			startActivity(intent2);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private class ClickListener implements View.OnClickListener{
		@Override
		public void onClick(View v) {
			int viewId = v.getId();
			switch (viewId) {
			case R.id.latest_list_refresh:
			case R.id.latest_list_refershTime:	//点击刷新
				Toast.makeText(c, "点击了刷新", 0).show();
				break;
			case 10:	//点击刷新
				Toast.makeText(c, "点击了获取更多", 0).show();
				break;

			}
		}
		
	}
}
